package com.leetcode.partition5;

import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2021/8/28 8:39
 */
public class LC455分发饼干 {

    public static int findContentChildren(int[] g, int[] s) {
        return greedy(g, s);
    }

    public static void main(String[] args) {
        int[] g = {1, 2}, s = {1, 2, 3};
        System.out.println(findContentChildren(g, s));
    }

    private static int greedy(int[] g, int[] s) {
        //先对胃口数组和饼干尺寸进行排序，并从后向前同时遍历，尽可能的让大饼干分发给胃口大的学生
        Arrays.sort(g);
        Arrays.sort(s);
        int answer = 0;
        for (int i = g.length - 1, j = s.length - 1; i >= 0 && j >= 0; i--) {
            if (s[j] >= g[i]) {         //能够满足当前孩子，饼干分发给他
                j--;
                answer++;
            }
        }
        return answer;
    }
}
